/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.service.impl;

import cn.hutool.json.JSONUtil;
import com.anwen.mongo.conditions.query.LambdaQueryChainWrapper;
import com.anwen.mongo.service.impl.ServiceImpl;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.excel.utils.ExcelUtil;
import com.comac.ins.isfd.constant.enums.IsfdMetadataTableNameEnum;
import com.comac.ins.isfd.domain.IsfdOperator;
import com.comac.ins.isfd.domain.IsfdOperatorParameterConfig;
import com.comac.ins.isfd.domain.bo.IsfdOperatorParameterConfigBo;
import com.comac.ins.isfd.domain.vo.IsfdOperatorParameterConfigImportVo;
import com.comac.ins.isfd.domain.vo.IsfdOperatorParameterConfigVo;
import com.comac.ins.isfd.service.IIsfdMetadataImportService;
import com.comac.ins.isfd.service.IIsfdOperatorParameterConfigService;
import com.comac.ins.isfd.service.IIsfdOperatorService;
import com.comac.ins.isfd.service.IIsfdParameterConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

/**
 * 算子参数配置service接口实现类
 *
 * @author hxloongs
 * @date 2024-7-9
 */
@Slf4j
@Service
public class IsfdOperatorParameterConfigServiceImpl extends ServiceImpl<IsfdOperatorParameterConfig> implements IIsfdOperatorParameterConfigService, IIsfdMetadataImportService {
    @Lazy
    @Autowired
    private IIsfdParameterConfigService iIsfdParameterConfigService;
    @Lazy
    @Autowired
    private IIsfdOperatorService iIsfdOperatorService;

    @Override
    public String selectByTableName() {
        return IsfdMetadataTableNameEnum.OPERATOR_PARAMETER_CONFIG.getValue();
    }

    @Override
    public Long excelImport(File excelImportFile, String metadataManagementId) {
        long successCount = 0L;

        try (InputStream is = new FileInputStream(excelImportFile)) {
            List<IsfdOperatorParameterConfigImportVo> targetVos = ExcelUtil.importExcel(is, IsfdOperatorParameterConfigImportVo.class);
            for (IsfdOperatorParameterConfigImportVo importVo : targetVos) {
                IsfdOperatorParameterConfigBo bo = MapstructUtils.convert(importVo, IsfdOperatorParameterConfigBo.class);
                try {
                    String operatorName = importVo.getOperatorName();
                    LambdaQueryChainWrapper<IsfdOperator> lambdaQueryChainWrapper = iIsfdOperatorService.lambdaQuery();
                    lambdaQueryChainWrapper.eq(IsfdOperator::getOperatorName, operatorName);
                    IsfdOperator isfdOperator = iIsfdOperatorService.one(lambdaQueryChainWrapper);
                    if (isfdOperator == null) {
                        throw new BaseException("算子名为：" + operatorName + " 不存在，请检查数据，并重新导入失败数据！");
                    }

                    String operatorRefId = isfdOperator.get_id();
                    // 幂等性操作
                    IsfdOperatorParameterConfigVo isfdOperatorParameterConfigVo = iIsfdParameterConfigService.one(operatorRefId, importVo.getPath(),
                        importVo.getType(), importVo.getFieldId(), importVo.getFieldType());
                    if (isfdOperatorParameterConfigVo != null) {
                        throw new BaseException("算子名为：" + operatorName + " 的数据已经存在，不可重复写入，请修改后重试！！");
                    }
                    // 设置自定义参数
                    String customJsonField = importVo.getCustomJsonField();
                    if (StringUtils.isNotBlank(customJsonField)) {
                        bo.setCustomJsonFieldObject(JSONUtil.parseObj(customJsonField));
                    }

                    // 设置元数据管理id
                    bo.setMetadataManagementId(metadataManagementId);
                    iIsfdParameterConfigService.parameterConfigOperatorAdd(bo);
                    successCount++;
                } catch (Exception e) {
                    log.error("参数库算子参数数据导入失败", e);
                }
            }

        } catch (Exception e) {
            log.error("sheet文件转换为实体类失败", e);
            throw new BaseException("sheet文件转换为实体类失败");
        }

        return successCount;
    }
}
